#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig.rb'

DOCKER_CCI = '/c/compass-ci'
DOCKER_LKP = '/c/lkp-tests'

names = Set.new %w[
  MQ_HOST
  MQ_PORT
  LKP_SERVER
  ES_USER
  ES_PASSWORD
]

defaults = relevant_defaults(names)
defaults['MQ_HOST'] ||= defaults['LKP_SERVER'] ||= '172.17.0.1'
defaults['LKP_SERVER'] ||= '172.17.0.1'
defaults['MQ_PORT'] ||= 5672
defaults['lab'] ||= 'z9'

service_authentication = relevant_service_authentication(names)
defaults.merge!(service_authentication)
env = docker_env(defaults)

docker_rm 'src-oepkgs-management'

cmd = %w[
  docker run
  --restart=always
  --name src-oepkgs-management
  -u 1090:1090
] + env + %W[
  -d
  -e CCI_SRC=#{DOCKER_CCI}
  -e LKP_SRC=#{DOCKER_LKP}
  -v #{ENV['LKP_SRC']}:#{DOCKER_LKP}
  -v #{ENV['CCI_SRC']}:#{DOCKER_CCI}
  -v /etc/localtime:/etc/localtime:ro
  -v /etc/compass-ci/defaults:/etc/compass-ci/defaults:ro
  -v /etc/compass-ci/register:/etc/compass-ci/register:ro
  -v /etc/compass-ci/service:/etc/compass-ci/service:ro
  -v /srv/git:/srv/git:ro
  -w /c/compass-ci/sbin
  alpine:src-oepkgs-management
]

cmd += ['sh', '-c', 'umask 002 && ./src-oepkgs-management.rb']
system(*cmd)
